package com.ysy.tz.core.engine;

import org.springframework.core.io.support.*;
import java.util.*;
import org.springframework.core.io.*;
import org.apache.ibatis.io.*;
import org.dom4j.io.*;
import org.apache.ibatis.session.*;
import org.dom4j.*;
import java.nio.charset.*;
import org.apache.commons.io.*;
import org.apache.commons.lang3.*;
import com.ysy.tz.core.utils.*;
import java.io.*;

public class ConfigLoader {
    static PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver;
    private static Properties properties;
    private static File defualtChoiseFile;

    public static InputStream loadFileFromOutsideAndInside(final String floderName, final String fileName) throws IOException {
        Resource rs = ConfigLoader.pathMatchingResourcePatternResolver.getResource("file:./" + floderName + "/" + fileName);
        if (!rs.getFile().exists()) {
            rs = ConfigLoader.pathMatchingResourcePatternResolver.getResource("file:./" + fileName);
            if (!rs.getFile().exists()) {
                try {
                    rs = ConfigLoader.pathMatchingResourcePatternResolver.getResource("classpath:/" + floderName + "/" + fileName);
                    rs.getFile().exists();
                } catch (Exception e) {
                    rs = ConfigLoader.pathMatchingResourcePatternResolver.getResource("classpath:" + fileName);
                }
            }
        }
        if (rs != null) {
            return rs.getInputStream();
        }
        return null;
    }

    public static void load() throws Exception {
        final InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        try {
            final Properties properties = new Properties();
            final InputStream fis = loadFileFromOutsideAndInside("config", "application.properties");
            properties.load(fis);
            fis.close();
            ApplicationContext.enterpriseId = Integer.parseInt(properties.getProperty("enterpriseId"));
            ApplicationContext.enterpriseCode = properties.getProperty("enterpriseCode");
            ApplicationContext.machineCode = properties.getProperty("machineCode");
            ApplicationContext.onlyShowGrading = properties.getProperty("onlyShowGrading").equals("true");
        } catch (Exception e) {
            e.printStackTrace();
        }
        final SAXReader saxReader = new SAXReader();
        final Document document = saxReader.read(inputStream);
        final Element mappersElement = document.getRootElement().addElement("mappers");
        final Resource[] resources2;
        final Resource[] resources = resources2 = ConfigLoader.pathMatchingResourcePatternResolver.getResources("classpath*:sqlmapper/*.xml");
        for (final Resource resource : resources2) {
            mappersElement.addElement("mapper").addAttribute("url", resource.getURI().toString());
        }
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final XMLWriter writer = new XMLWriter((OutputStream) baos, OutputFormat.createCompactFormat());
        writer.write(document);
        writer.close();
        final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        final SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        ApplicationContext.sqlSessionFactory = sqlSessionFactoryBuilder.build((InputStream) swapStream);
    }

    public static String readDefaultChoiseConfigProperties(final String key) {
        if (ConfigLoader.defualtChoiseFile == null) {
            ConfigLoader.defualtChoiseFile = checkDefaultChoiseFile();
            try {
                final String str = FileUtils.readFileToString(ConfigLoader.defualtChoiseFile, Charset.forName("UTF-8"));
                if (StringUtils.isNotBlank((CharSequence) str)) {
                    ConfigLoader.properties.load(new StringReader(AESUtil.Dcodes(str)));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return String.valueOf(ConfigLoader.properties.getProperty(key));
    }

    public static void saveDefaultChoiseConfigProperties(final String key, final String value) {
        ConfigLoader.properties.setProperty(key, value);
        final StringWriter sw = new StringWriter();
        try {
            ConfigLoader.properties.store(sw, "");
            final String str = AESUtil.Ecodes(sw.toString());
            FileUtils.writeStringToFile(ConfigLoader.defualtChoiseFile, str, Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static File checkDefaultChoiseFile() {
        final String fileName = System.getProperty("user.dir") + File.separator + "config" + File.separator + "default.ini";
        final File file = new File(fileName);
        if (!file.exists()) {
            try {
                FileUtils.touch(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    static {
        ConfigLoader.pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
        ConfigLoader.properties = new Properties();
    }
}
